using System;
using System.IO;
using Gtk;

namespace animeshell
{
	
	
	public class Preference : Gtk.Window
	{
		protected Gtk.Entry txtNavigateur;
		protected Gtk.Button btnSelectNavigateur;
		protected Gtk.Button btnQuitter;
		protected Gtk.Button btnAnnuler;
		protected Gtk.Button btnSave;
		protected Gtk.ComboBoxEntry cbMoteur;
		private string m_NomNavigateur;
		private Traitement m_CTraitement;
		private FileSelection fs;

		
		public Preference(Traitement CT) : base("")
		{
			Stetic.Gui.Build(this, typeof(animeshell.Preference));
			m_CTraitement = CT;
		}

		protected virtual void btnQuitter_Click(object sender, System.EventArgs e)
		{
			this.Destroy();
		}

		protected virtual void btnAnnuler_Click(object sender, System.EventArgs e)
		{
			this.Destroy();
		}

		protected virtual void btnSave_Click(object sender, System.EventArgs e)
		{
			// Variables
			FileStream fstream = null;
			StreamWriter sw = null;
			
			// Écriture
			try
			{
				fstream = new FileStream(m_CTraitement.rc, FileMode.Create);
				sw = new StreamWriter(fstream);
				
				sw.WriteLine(m_NomNavigateur);
				sw.WriteLine(cbMoteur.ActiveText);
			}
			catch(Exception EX)
			{
				MessageDialog md = new MessageDialog (null,DialogFlags.DestroyWithParent,
				                       MessageType.Error,ButtonsType.Close,
				                       "Erreur lors de l'écriture des préférences.\r\n\r\n"+EX.ToString());
				md.Run();
				md.Destroy();
			}
			finally
			{
				if (sw != null)
					sw.Close();
				if (fstream != null)
					fstream.Close();
			}
			m_CTraitement.LoadRc();
			this.Destroy();
		}

		protected virtual void btnNavigateur(object sender, System.EventArgs e)
		{
			fs = new FileSelection("Sélection de votre navigateur Internet");
			fs.HideFileopButtons();
			fs.Response += new ResponseHandler (OnFileSelectionResponse);		
			fs.Run();
		}
		
		protected void OnFileSelectionResponse (object o, ResponseArgs args)
		{
			try
			{
				if (args.ResponseId == ResponseType.Ok)
        		{
          			m_NomNavigateur = fs.Filename;
        			fs.Hide();
        			txtNavigateur.Text = m_NomNavigateur;
          		}
          		else
          		{
          			fs.Hide();
          		}
			}
			catch(Exception EX)
			{
				MessageDialog md = new MessageDialog (null,DialogFlags.DestroyWithParent,MessageType.Error, 
			                                         ButtonsType.Close, "Erreur lors de la sélection.\r\n\r\n"+EX.ToString());
				md.Run();
				md.Destroy();
			}
		}
	}
}
